package com.test.web.dao.impl;

import java.awt.List;
import java.nio.file.PathMatcher;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Set;
import com.test.web.dao.OrderDao;
import com.test.web.pojo.Orders;
import com.test.web.util.BaseDao;
/**
 * 
 * @author 张迦南
 *
 */
public class OrderDaoImpl extends BaseDao implements OrderDao{

	Connection connection = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	

//*********************************妫�楠岀櫥褰曠敤鎴峰悕鏄惁姝ｇ‘*****************************
	public boolean queryUser(String name, String password) {
		// TODO Auto-generated method stub
		
		boolean flag=false;
		
		
				try {
					connection = getConnection();
					
					String sql="select * from user where username=? and userpwd=?";
					
					pstmt=connection.prepareStatement(sql);
					pstmt.setString(1, name);
					pstmt.setString(2, password);
					rs=pstmt.executeQuery();
					if(rs.next()) {
						flag=true;
						
					}
					
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println("111");
					e.printStackTrace();
				}finally {
					try {
						rs.close();
						pstmt.close();
						connection.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
	
		return flag;
	}

	//*********************************鐢ㄦ埛鏌ヨ鎴戠殑璁㈠崟淇℃伅骞跺垎椤垫樉绀�*****************************
	@Override
	public ArrayList<Orders> myOrder(int userid,int page,int rows) {
		// TODO Auto-generated method stub
		ArrayList<Orders> orderlist = new ArrayList<>();
		int start = (page-1)*rows;
		connection=getConnection();
		
		String sql="select orderid,userid,username,ticketid,orderstate,price,ordertime from orders where userid=? limit ?,?";
;
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, userid);
			pstmt.setInt(2, start);
			pstmt.setInt(3, rows);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				Orders orders=new Orders();				
				orders.setOrderid(rs.getInt("orderid"));
				orders.setUserid(rs.getInt("userid"));
				orders.setUsername(rs.getString("username"));
				orders.setTicketid(rs.getInt("ticketid"));
		        orders.setOrderstate(rs.getString("orderstate"));
				orders.setPrice(rs.getDouble("price"));
				orders.setOrdertime(rs.getTimestamp("ordertime"));
				orderlist.add(orders);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		return orderlist;
	}


	@Override
	public int queryidByusername(String name) {
		// TODO Auto-generated method stub
		int id=0;
		connection=getConnection();
		String sql="select userid from user where username=?";
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setString(1, name);
			rs=pstmt.executeQuery();
			if(rs.next()) {
			id=rs.getInt("userid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
				
		return id;
	}


	@Override
	public int  takeOrder(int userid,int orderid) {
		// TODO Auto-generated method stub
		connection=getConnection();
		int result=0;
		String sql="update orders set orderstate='已取票' where userid=? and orderid=? and orderstate like '%已付款%'";
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, userid);
			pstmt.setInt(2, orderid);
			result=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	
	}


	@Override
	public String queryCardid(int userid) {
		// TODO Auto-generated method stub
		connection=getConnection();
		String  cardid=null;
		String sql="select cardid from user where userid=?";
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, userid);
			rs=pstmt.executeQuery();
			if(rs.next()) {
			cardid=rs.getString("cardid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return cardid;
	}


	@Override
	public int cancelTicket(int userid,int orderid) {
		// TODO Auto-generated method stub
		
		connection=getConnection();
		int result=0;
		String sql="update orders set orderstate='已退票' where userid=? and orderid=? and orderstate not like '%已取票%'";
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, userid);
			pstmt.setInt(2, orderid);
			result=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return result;
	}


	@Override
	public boolean editOrder(int orderid) {
		// TODO Auto-generated method stub
		boolean flag=false;
		connection=getConnection();
		String sql="delete from orders where orderid=? and orderstate like '%已付款%'";
		try {
			pstmt=connection.prepareStatement(sql);
			pstmt.setInt(1, orderid);
			pstmt.executeUpdate();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			try {
				
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return true;
	}

	@Override
	public int queryTotal() {
		// TODO Auto-generated method stub
		int total=0;
		connection=getConnection();
		String sql="select count(*) from orders";
		try {
			pstmt=connection.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs.next()) {
				total=rs.getInt(1);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		return total;
	}


}